#!/bin/bash
# podspec文件 转 podspec.json文件，并拷贝到相应的Specs目录下

# 获取spec版本号
ct_getSpecFileVersion() {
	fileName=$1
	
	cat ${fileName} | while read line
	do
		array=(${line//=/ })
		
		if [[ ${#array[@]} == 2 ]]; then
			if [[ ${array[0]} == *".version" ]];  then
				echo ${array[1]} |sed "s/[\"\']//g" # 去除两端的双引号
				break;
			fi
		fi
	done
}

# 获取本地私有仓库的目录
ct_getPrivateSpecsFolder() {
	tmp_folders=("creatoo" "andney" "gitee-andney-pod-specs" "pod-specs")
	for item in ${tmp_folders[*]}; do
		a="${HOME}/.cocoapods/repos/${item}/Specs"
		if [ -d "${a}" ]; then
			echo "${a}"
			return 1;
		fi
	done;
	
	return 0;
}


specs_folder="$(ct_getPrivateSpecsFolder)"
if [ ${#specs_folder} == 0 ]; then
	echo "私有Podspec仓库文件夹不存在...."
	exit 0;
fi

# 遍历podsepc文件
for pod in `ls *.podspec`
do
	spec_name=${pod%.*} # 去掉后缀
	spec_ver=$(ct_getSpecFileVersion ${pod})
	if [[ ${#spec_ver} > 0 ]]; then
		fold="${spec_name}/${spec_ver}"
	else
		continue
	fi
	
	targetFolder="${specs_folder}/${fold}"
	if [ ! -d "${targetFolder}" ];then
		mkdir -p "${targetFolder}"
	fi
	
	targetPath="${targetFolder}/${pod}.json"
	if [ -f "${targetPath}" ];then
		rm "${targetPath}"
	fi
	
	pod ipc spec $pod >> "${targetPath}"

	echo $spec_name
done
